package com.taoyibao.hongcanting.ui.admin.views;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.event.EventUpdataTask;
import com.taoyibao.hongcanting.model.TaskModel;
import com.taoyibao.hongcanting.utils.CodeUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * @author TangAnna
 * @description: 屏幕操作员的item
 * @date :${DATA} 13:56
 */
public class ItemAdminArea extends BaseRelativeLayout<TaskModel> implements View.OnClickListener {
    /**
     * 任务状态
     */
    private int mStatus;

    /**
     * 序号
     */
    private TextView mTvNub;
    /**
     * 桌号
     */
    private TextView mTvTable;
    /**
     * 发送时间
     */
    private TextView mTvSendTime;
    /**
     * 任务种类和时长  title
     */
    private TextView mTvTypeAndDuration;
    /**
     * 任务种类 和时长的值
     */
    private TextView mTvTypeAndDurationValue;
    /**
     * 霸屏最外层布局
     */
    private LinearLayout mLayoutBaPing;
    /**
     * 霸屏的文字
     */
    private TextView mTvBaPingText;
    /**
     * 计时器
     */
    private TextView mTvTimer;
    /**
     * 确认按钮
     */
    private Button mBtnConfirm;
    /**
     * 发送消息的时间
     */
    private long mSendTime;
    private String mHoursStr;
    private String mMinutesStr;
    private String mSecondStr;

    private TaskModel mTaskModel;

    public ItemAdminArea(Context context) {
        super(context);
    }

    public ItemAdminArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemAdminArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mTvNub = findViewById(R.id.tv_itemAdmin_nub);
        mTvTable = findViewById(R.id.tv_itemAdmin_table);
        mTvSendTime = findViewById(R.id.tv_itemAdmin_sendTime);
        mTvTypeAndDuration = findViewById(R.id.tv_itemAdmin_typeAndDuration);
        mTvTypeAndDurationValue = findViewById(R.id.tv_itemAdmin_typeAndDurationValue);
        mLayoutBaPing = findViewById(R.id.layout_itemAdmin_baPing);
        mTvBaPingText = findViewById(R.id.tv_itemAdmin_baPingText);
        mTvTimer = findViewById(R.id.tv_itemAdmin_timer);
        mBtnConfirm = findViewById(R.id.btn_admin_confirm);
        mBtnConfirm.setOnClickListener(this);
    }

    @Override
    public void setData(TaskModel data) {

    }

    /**
     * @param data
     * @param status 状态
     */
    public void setData(TaskModel data, int status, int position) {
        mTaskModel = data;
        mStatus = data.status;
        //根据不同状态先控制页面显示
        switch (mStatus) {
            case CodeConstant.TASK_STATUS_NEW://新任务
                mBtnConfirm.setVisibility(VISIBLE);
                mBtnConfirm.setText(CodeUtils.getIdString(R.string.admin_confirm));
                mBtnConfirm.setBackgroundResource(R.drawable.shape_bg_raduis_ffd);
                break;
            case CodeConstant.TASK_STATUS_ING://进行中
                mBtnConfirm.setVisibility(VISIBLE);
                mBtnConfirm.setText(CodeUtils.getIdString(R.string.admin_complete));
                mBtnConfirm.setBackgroundResource(R.drawable.shape_bg_raduis_ff9);
                break;
            case CodeConstant.TASK_STATUS_FINISH://已完成
                mBtnConfirm.setVisibility(GONE);
                break;
        }
        //设置序号
        mTvNub.setText((position + 1) + "");
        //桌号
        if (!TextUtils.isEmpty(data.desk_code)) {
            mTvTable.setText(data.desk_code);
        } else {   //游客
            mTvTable.setText(CodeUtils.getIdString(R.string.admin_tourist));
        }
        //发送时间
        mTvSendTime.setText(CodeUtils.formatDateSecond(data.create_at));
        //礼物类型
        if (TextUtils.equals(data.type, "大屏")) {//大屏
            mLayoutBaPing.setVisibility(VISIBLE);
            if (!TextUtils.isEmpty(data.content)) {
                mTvBaPingText.setText(data.content);
            }
            mTvTypeAndDuration.setText("时长:");
            mTvTypeAndDurationValue.setText(data.gift_name + "分钟");
        } else {//正常礼物
            mLayoutBaPing.setVisibility(GONE);
            mTvTypeAndDuration.setText(data.type + ":");
            mTvTypeAndDurationValue.setText(data.gift_name);
        }
    }

    /**
     * 获取Tag
     *
     * @return
     */
    public TextView getTag() {
        return (TextView) mTvTimer.getTag();
    }


    @Override
    protected int bindLayout() {
        return R.layout.item_admin;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_admin_confirm:
                //发送消息到AdministratorFragment中调用接口更新任务状态  刷新页面
                EventUpdataTask task = new EventUpdataTask();
                task.mTaskModel = mTaskModel;
                if (mStatus == 1) {//确认操作
                    task.status = 2;
                } else if (mStatus == 2) {//完成操作
                    task.status = 3;
                }
                EventBus.getDefault().post(task);
                break;
        }
    }
}
